
#include <curses.h>
#include <sys/types.h>
#include <vector>
#include <iostream>
#include <fstream>

#include "include/startWin.h"
#include "include/renderBMP.h"
#include "include/types.h"

using namespace std;

void createBMP(vector<uint32> &size, vector<vector<vector<uchar8>>> &BMPmap) {
    std::ofstream os("temp.bmp", std::ios::binary);

    uchar8 signature[2] = { 'B', 'M' };
    uint32 fileSize = 14 + 40 + 100*100*4;
    uint32 reserved = 0;
    uint32 offset = 14 + 40;

    uint32 headerSize = 40;
    // uint32 dimensions[2] = { 100, 100 };
    uint16 colorPlanes = 1;
    uint16 bpp = 32;
    uint32 compression = 0;
    uint32 imgSize = 100*100*4;
    uint32 resolution[2] = { 2795, 2795 };
    uint32 pltColors = 0;
    uint32 impColors = 0;

    os.write(reinterpret_cast<char*>(signature), sizeof(signature));
    os.write(reinterpret_cast<char*>(&fileSize), sizeof(fileSize));
    os.write(reinterpret_cast<char*>(&reserved), sizeof(reserved));
    os.write(reinterpret_cast<char*>(&offset),   sizeof(offset));

    os.write(reinterpret_cast<char*>(&headerSize),  sizeof(headerSize));
    os.write(reinterpret_cast<char*>(&size[0]),   sizeof(size[0]));
    os.write(reinterpret_cast<char*>(&size[1]),   sizeof(size[1]));
    os.write(reinterpret_cast<char*>(&colorPlanes), sizeof(colorPlanes));
    os.write(reinterpret_cast<char*>(&bpp),         sizeof(bpp));
    os.write(reinterpret_cast<char*>(&compression), sizeof(compression));
    os.write(reinterpret_cast<char*>(&imgSize),     sizeof(imgSize));
    os.write(reinterpret_cast<char*>(resolution),   sizeof(resolution));
    os.write(reinterpret_cast<char*>(&pltColors),   sizeof(pltColors));
    os.write(reinterpret_cast<char*>(&impColors),   sizeof(impColors));

    uchar8 x,r,g,b;

    for (unsigned long int i = 0; i < BMPmap.size(); i++) {
        for (unsigned long int j = 0; j < BMPmap[i].size(); j++) {
            for (unsigned long int k = 0; k < BMPmap[i][j].size(); k++) {

                for (int i=0; i < (int)size[0]; i++) {
                    for (int j=0; j < (int)size[1]; j++) {
                        x = 0;

                        r = BMPmap[i][j][0];
                        g = BMPmap[i][j][1];
                        b = BMPmap[i][j][2];

                        os.write(reinterpret_cast<char*>(&b),sizeof(b));
                        os.write(reinterpret_cast<char*>(&g),sizeof(g));
                        os.write(reinterpret_cast<char*>(&r),sizeof(r));
                        os.write(reinterpret_cast<char*>(&x),sizeof(x));
                    }
                }

            }
        }
    }

    os.close();
}

int main() {
    
    int y;
    int x;

    // 0 - width
    // 1 - height
    vector<uint32> sizeBMP(2);

    initscr();
    getmaxyx(stdscr, y, x);

    startMenu(sizeBMP, y, x);
    clear();
    
    vector<vector<vector<uchar8>>> BMPmap(sizeBMP[1], vector<vector<uchar8>>(sizeBMP[0], vector<uchar8>(3, 0)));
    renderBMPScreen(BMPmap, y, x);
    
    createBMP(sizeBMP, BMPmap);

    endwin();
    
    return 0;
}